<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 4, 2013
 * Time: 4:05:34 PM
 * To change this template use File | Settings | File Templates.
 */

class Department {
    private $db;
    private $id;
    private $departmentName;
    private $departmentCode;
    private $status;
    private $isDelete;
    private $version;

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
    }

    public function testPrint() {
        $pdfHelper = new PrintHelper();
        $pdf = $pdfHelper->getPage();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(40, 10, 'Hello World! PDF');
        $pdf->Output();
    }

    public function GetSectionListMenu() {
        $sql = 'select name,id from aea_department';
        $sectionMenuList = $this->db->Execute($sql);
        return $sectionMenuList->GetMenu2('searchCriteria.section', '', ':--All select--', '', '', 'id="searchCriteria.section" onchange="doSearch(event)" style="width:150px;"');
    }

    public function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $department = new DepartmentAR();
            if ($this->id != '' AND $this->id != NULL) {
                $department->Load("id=$this->id");
            } else {
                $department->create_time = date('Y-m-d H:i:s', time());
                $department->create_user_id = $_SESSION['user_id'];
                $department->is_delete = 0;
            }
            $department->dep_code = $this->departmentCode;
            $department->name = $this->departmentName;
            $department->status = $this->status;
            $department->update_user_id = $_SESSION['user_id'];
            $department->version = $this->version;
            $i = $department->save();
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getDepartmentOptions() {
        $sql = 'select name,id,dep_code from aea_department';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                    " . $res->fields['dep_code'] . " : " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getInstituteOptions() {
        $sql = 'select name,id from aea_institute';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                     " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getLocationOptions() {
        $sql = 'select id,name from aea_location';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                     " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getDepartment($roleId, $code = true) {
        /*
        if($code){
           $sql = "select aea_privilege.id, code from aea_privilege Where aea_privilege.is_delete=0
                AND aea_privilege.id IN (SELECT distinct privilege_id from aea_role_privilege where aea_role_privilege.role_id ='$roleId')";
        }else{
          $sql = "SELECT distinct id,privilege_id from aea_role_privilege where aea_role_privilege.role_id ='$roleId' order by privilege_id asc";  
        }

        $allPrivilege= $this->db->Execute($sql);
        return $allPrivilege->GetAssoc();
    }
    public function getNotPrivilege($roleId){
        $sql = "select aea_privilege.id, code from aea_privilege Where aea_privilege.is_delete=0
                AND aea_privilege.id NOT IN (SELECT distinct privilege_id from aea_role_privilege where aea_role_privilege.role_id ='$roleId')";
        $allPrivilege= $this->db->Execute($sql);
        return $allPrivilege->GetAssoc();
    }
    public function assignRoleToPrivilege($add_values,$delete_values,$role_id){
        if(!empty($delete_values) ){
             $delArray = array_values($delete_values);
             $sql="delete from aea_role_privilege where role_id = $role_id AND  privilege_id IN (".implode(', ', $delArray).")" ;
            $resultDelete= $this->db->Execute($sql);
            $delStatus=true;
        }else
            $delStatus=false;
        if(!empty($add_values)){
           $addArray = array_values($add_values);
           $uid=$_SESSION['user_id'];
           $create_time =date('Y-m-d H:i:s', time());
            $sql=  "INSERT INTO aea_role_privilege(role_id,create_user_id,update_user_id,create_time,privilege_id) values($role_id,$uid,$uid,'$create_time',?)";
            for ($i = 0; $i < count($addArray); $i++) {
                $resultAdd = $this->db->Execute($sql, $addArray[$i]);
            }
            $addStatus=true;
        }else
            $addStatus=false;
        $results=array('add'=>$resultAdd,'del'=>$resultDelete,'detStatus'=>$delStatus,'addStatus'=>$addStatus);
        return $results;
       // return $allPrivilege->GetAssoc();
       * */
    }

    public function getGrid($posStart, $count, $name, $status, $sort_by, $sort_dir) {
        $sql = "SELECT id,dep_code,name,status FROM aea_department Where is_delete=0";
        if ($name != '')
            $sql .= " and name like '$name%'";
        if ($status != '')
            $sql .= " and status = '$status'";

        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['id'] . "'>
                    <cell>" . $res->fields['id'] . "</cell>
                    <cell>" . $res->fields['dep_code'] . "</cell>
                    <cell>" . $res->fields['name'] . "</cell>
                    <cell>" . ($res->fields['status'] == 1 ? "Active" : "Incative") . "</cell>
                    <cell>" . $res->fields['status'] . "</cell>
                    </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }
}

require_once('libraries/adodb5/adodb-active-record.inc.php');
class DepartmentAR extends ADOdb_Active_Record {
    var $_table = 'aea_department';

}
